package cn.yanxiaohui.service.web.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.yanxiaohui.service.web.config.ApplicationConfiguraion;

/**
 * 过滤请求，为请求设置参数
 * @author yanxiaohui
 *
 */
@Component
@WebFilter(filterName = "appInitFilter", urlPatterns = "/*")
public class AppInitFilter implements Filter {

	@Autowired	
	private ApplicationConfiguraion applicationConfiguraion;
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 设置nginx的url
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		this.setCommonUrl(request);
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	 private void setCommonUrl(ServletRequest request) {
		 request.setAttribute("staticServer", "http://" + applicationConfiguraion.getStaticServer());
	 }

}